package com.jackrain.nea.fc.vp.service;

import com.alibaba.dubbo.config.annotation.Service;
import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.alibaba.fastjson.parser.Feature;
import com.alibaba.fastjson.serializer.SerializerFeature;
import com.alibaba.fastjson.util.TypeUtils;
import com.jackrain.nea.exception.NDSException;
import com.jackrain.nea.fc.utils.Constants;
import com.jackrain.nea.fc.vp.api.ViewCvipUpQueryCmd;
import com.jackrain.nea.fc.vp.mapper.ViewCvipUpMapper;
import com.jackrain.nea.sys.CommandAdapter;
import com.jackrain.nea.util.ApplicationContextHandle;
import com.jackrain.nea.util.ValueHolder;
import lombok.extern.slf4j.Slf4j;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Component;

import java.sql.Timestamp;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;

/**
 * FC-会员接口-VIP升级-FC升级流水 —> F4（伯俊）
 *
 * @author WSL
 * @create 2017/11/17
 */
@Slf4j
@Component
@Service(protocol = "dubbo" , validation = "true" , version = "1.0" , group = "vp")
public class ViewCvipUpQueryCmdImpl extends CommandAdapter implements ViewCvipUpQueryCmd {

    @Override
    public ValueHolder execute(HashMap map) throws NDSException {
        ValueHolder vh = new ValueHolder();
        Long startTime = System.currentTimeMillis();
        // Mapper
        ViewCvipUpMapper viewCvipUpMapper = ApplicationContextHandle.getBean(ViewCvipUpMapper.class);

        JSONObject param = JSON.parseObject(JSON.toJSONStringWithDateFormat(map.get("param"), "yyyy-MM-dd HH:mm:ss", SerializerFeature.WriteMapNullValue), Feature.OrderedField);
        log.info("ViewCvipUpQueryCmdImpl--param-->{}",param);

        if (null == param) {
            throw new NDSException("参数格式错误！");
        }

        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");

        // 查询【VIP升级日志】表
        List<HashMap> list;
        if (null != param.getInteger("row")){
            if (param.getInteger("row") > 100){
                param.put("row",100);
            }
            list = viewCvipUpMapper.selectProviderByRow(param);
        }else{
            list = viewCvipUpMapper.selectProvider(param);
        }
        JSONArray resultJson = new JSONArray();
        for (HashMap hashMap: list) {
            // VIP卡号
            String vipId = viewCvipUpMapper.getVipAccEcode(TypeUtils.castToLong(hashMap.get("VP_C_VIP_ACC_ID")),TypeUtils.castToLong(hashMap.get("VP_C_VIP_ID")));
            hashMap.put("VP_C_VIP_ACC_ID",vipId);

            // 升级前等级
            String vipTypeIdOld = viewCvipUpMapper.getVipTypeEcode(TypeUtils.castToLong(hashMap.get("VP_C_VIPTYPE_OLDID")));
            hashMap.put("VP_C_VIPTYPE_OLDID",vipTypeIdOld);

            // 升级后等级
            String vipTypeIdNew = viewCvipUpMapper.getVipTypeEcode(TypeUtils.castToLong(hashMap.get("VP_C_VIPTYPE_NEWID")));
            hashMap.put("VP_C_VIPTYPE_NEWID",vipTypeIdNew);

            // 升级日期
            hashMap.put("OP_DATE",sdf.format(TypeUtils.castToDate(hashMap.get("DATEUP").toString())));

            // 创建人
            hashMap.put("OWNERID",hashMap.get("OWNERENAME"));

            // 类型
            Long type = TypeUtils.castToLong(hashMap.get("TYPE"));
            if (1 == type){
                hashMap.put("TYPE",0);
            }else if (2 == type){
                hashMap.put("TYPE",1);
            }
            // 备注
            hashMap.put("REMARK",hashMap.get("REMARK"));

            JSONObject jsonObject = JSON.parseObject(JSON.toJSONStringWithDateFormat(new JSONObject(hashMap),"yyyy-MM-dd HH:mm:ss", SerializerFeature.WriteMapNullValue), Feature.OrderedField);
            resultJson.add(jsonObject);
        }

        Long endTime = System.currentTimeMillis();
        vh.put("data",resultJson);
        vh.put("spendTime",endTime-startTime);
        return vh;
    }

}
